<?php

class DashboardController extends AdminController {

    public $db;
    
    public function actionIndex() {
        //die("test");
        $length_day = 30;
        $start_daily = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - $length_day, date("Y")));
        $start_weekly = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
        $start_monthly = date("Y-m-d", mktime(0, 0, 0, date("m") - 12, date("d"), date("Y")));
        $start_yearly = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y") - 3));
        /*
         * AKTIVASI
         */

       // $command_income = new Bonus_model();
        /*
         * DAILY REPORT
         */
        $results = $this->income_daily($start_daily);
        $data_arr = $this->results($results);
        
      
        /*
         * MONTHLY REPORT
         */
        $monthly_results = $this->income_monthly($start_monthly);
        $monthly_data = $this->results($monthly_results);
//  echo '<pre>';print_r($monthly_data);
//exit;
        /*
         * SUMMARY REPORT
         */

        $dataProviderIncome = array(
            array(
                'network_level_status_log_value' => 'Distributor',
                'Jumlah' => 0,
            ),
            array(
                'network_level_status_log_value' => 'Warehouse',
                'Jumlah' => 0,
            ),
            array(
                'network_level_status_log_value' => 'Factory',
                'Jumlah' => 0,
            ),
        );

        $results_income = $this->income_summary();
        $dataProviderValue = !empty($results_income) ? $results_income : $dataProviderIncome;
        $dataProviderIncome = new CArrayDataProvider($dataProviderValue, array(
                    'keyField' => 'network_level_status_log_value',
                    'sort' => array(
                        'attributes' => array('Jumlah'),
                    ),
                ));

        $this->render('index', array(
            'tanggal' => $data_arr['tanggal'],
            'jml_distributor' => $this->add_index($data_arr['jumlah']['distributor'], $value_add = $length_day),
            'jml_warehouse' => $this->add_index($data_arr['jumlah']['warehouse'], $value_add = $length_day),
            'jml_factory' => $this->add_index($data_arr['jumlah']['factory'], $value_add = $length_day),
            'monthly_data' => $monthly_data,
            'dataProviderIncome' => $dataProviderIncome,
        ));
    }

    /*
     * FUNGSI UNTUK NAMBAH INDEX ARRAY JIKA TIDAK MENCUKUPI NILAI MINIMAL
     */

    private function add_index($data_array, $value_add=30) {
        $count_data = count($data_array);
        if ($count_data < $value_add) {
            for ($i = $count_data; $i <= $value_add - 1; $i++) {
                $data_add[$i - 1] = 0;
            }
        }

        return array_merge($data_array, $data_add);
    }

    private function income_daily($start_daily) {
        
        $this->db = Yii::app()->db->createCommand();
        $data_arr = array(
            'select' => 'date(network_level_status_log_datetime) AS Tgl, `network_level_status_log_value`, count(`network_level_status_log_id`) AS Jumlah',
            'where' => 'network_level_status_log_value!=:value AND date(network_level_status_log_datetime)>=:start_daily',
            'params' => array(':value' => 'passive', ':start_daily' => $start_daily),
            'group' => 'network_level_status_log_value, date(network_level_status_log_datetime)',
        );
        ;
        //exit;

        $this->member_activation($data_arr);
        $results = $this->db->queryAll();
        return $results;
    }

    private function income_monthly($start_monthly) {
        $this->db = Yii::app()->db->createCommand();
        $data_arr = array(
            'select' => 'LEFT(network_level_status_log_datetime,7) AS Tgl, `network_level_status_log_value`, count(`network_level_status_log_id`) AS Jumlah',
            'where' => 'network_level_status_log_value!=:value AND LEFT(network_level_status_log_datetime,7)>=:start_monthly',
            'params' => array(':value' => 'passive', ':start_monthly' => $start_monthly),
            'group' => 'network_level_status_log_value, LEFT(network_level_status_log_datetime,7)',
        );
        ;
        //exit;

        $this->member_activation($data_arr);
        $results = $this->db->queryAll();
        return $results;
    }

    private function income_summary() {
        
        $data_arr = array(
            'select' => 'network_level_status_log_datetime AS Tgl, `network_level_status_log_value`, count(`network_level_status_log_id`) AS Jumlah',
            'where' => 'network_level_status_log_value!=:value',
            'params' => array(':value' => 'passive'),
            'group' => 'network_level_status_log_value',
        );
        ;
        //exit;

        $this->member_activation($data_arr);
        $results = $this->db->queryAll();
        return $results;
    }

    private function results($results) {
        $val = array();
        $tanggal = array();
        $jenis_aktivasi = array();
        $jumlah = array();


        if (!empty($results)) {

            foreach ($results AS $val) {
                $tanggal[] = $val['Tgl'];
                $jenis_aktivasi[] = $val['network_level_status_log_value'];
                if ($val['network_level_status_log_value'] == 'distributor') {
                    $jumlah['distributor'][] = intval($val['Jumlah']);
                }

                if ($val['network_level_status_log_value'] == 'factory') {
                    $jumlah['factory'][] = intval($val['Jumlah']);
                }
                if ($val['network_level_status_log_value'] == 'warehouse') {
                    $jumlah['warehouse'][] = intval($val['Jumlah']);
                }
            }
        }

        return array(
            'tanggal' => $tanggal,
            'jenis_aktivasi' => $jenis_aktivasi,
            'jumlah' => $jumlah,
        );
    }
    
     private function member_activation($data_arr)
    {
        $this->db = Yii::app()->db->createCommand();
        $this->db->select($data_arr['select']);
	$this->db->from('sys_network_level_status_log');
        if($data_arr['where']!='')
        {
            $this->db->where($data_arr['where'],$data_arr['params']);
        }
	       //  array('not in','network_level_status_log_value','passive'),
               // array('>=','date(network_level_status_log_datetime)',$start_daily)
        if(isset($data_arr['group']) AND trim($data_arr['group'])!='')
        {
            $this->db->group($data_arr['group']);
        }
        if(isset($data_arr['order']) AND trim($data_arr['order'])!='')
        {
            $this->db->order($data_arr['order']);
        }

        return $this;
    }

}
